/* ========================================== */
/* Copyright(c) 2016 TimesForus Corporation.  */
/*            All rights reserved.            */
/*          TIMESFORUS CONFIDENTIAL           */
/* ========================================== */
package com.baili.service.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baili.dao.user.UserDao;
import com.baili.dto.InsterOrganization;
import com.baili.dto.ProductOrder;
import com.baili.dto.SelectUserInput;
import com.baili.dto.common.UserConstants;
import com.baili.util.EhcacheUtil;
import com.baili.util.GetMobelNum;
import com.baili.util.StringUtil;

/**
 * TODO
 * <p>
 * This contains the following methods:<br/>
 * <li><code>handleCreateUser</code></li>
 * <li><code>handleUserLogin</code></li>
 * <li><code>idCardNumber</code></li>
 * <li><code>isExist</code></li>
 * <li><code>isOrNotAttestation</code></li>
 * <li><code>isRegist</code></li>
 * <li><code>modifyPassword</code></li>
 * <li><code>msmSend</code></li>
 * <li><code>recharge</code></li>
 * <li><code>resetPassword</code></li>
 * <li><code>updatePassword</code></li>
 * <p>
 * 
 * @see com.baili.dao.user.UserDao
 * @see com.baili.dao.userrecharge.UserRechargeDao
 * @see com.baili.dto.ObjectOutput
 * @see com.baili.dto.UserRecharge
 * @see com.baili.dto.common.UserConstants
 * @see com.baili.dto.vo.ZtUserInfoInput
 * @see com.baili.dto.vo.ZtUserInfoOutput
 * @see com.baili.service.pay.PayService
 * @see com.baili.util.StringUtil
 * @see org.apache.commons.lang3.StringUtils
 * 
 * @author Unisys
 * @version 1.0
 * @since 1.0
 */

@Service
public class UserServiceImpl implements UserService {

	/**
	 * Attribute to hold the <code>userDao</code> property.
	 */
	@Autowired
	private UserDao userDao;
	
	/**
	 * Attribute to hold the <code>ehcacheUtil</code> property.
	 */
	@Autowired
	private EhcacheUtil ehcacheUtil;
	
	/**
	 * Attribute to hold the <code>getMobelNum</code> property.
	 */
	@Autowired
	private GetMobelNum getMobelNum;
	
	/**
	 * 用户注册
	 * 
	 * <p>
	 * <b>Release Notes:</b> <br/>
	 * <table border="1" cellspacing="0" cellpadding="5" width="80%">
	 * <tr>
	 * <th align="left">Release</th>
	 * <th align="left">Notes</th>
	 * </tr>
	 * <tr>
	 * <td>@since 1.0</td>
	 * <td>&nbsp;</td>
	 * </tr>
	 * </table>
	 * <p>
	 * 
	 * @param sesionid
	 *            <code>String</code> session ID
	 * @param passwd
	 *            <code>String</code> 密码
	 * @param code
	 *            <code>String</code> 验证码
	 * @return <code>int</code> 成功或失败
	 * @throws Exception 
	 */
	@Override
	public int handleCreateUser(SelectUserInput selectUserInput) throws Exception {
		
		JSONObject json = new JSONObject();
		
		if (StringUtils.isEmpty(selectUserInput.getMobile()) || StringUtils.isEmpty(selectUserInput.getName())) {
			// 参数异常
			return UserConstants.PARAMETER_ERROR;
		}

		if (!StringUtil.isPhoneNum(selectUserInput.getMobile().trim())) {
			// 是否是手机号
			return UserConstants.FORMAT_ERROR;

		}
		
		Map<String, Object> map = userDao.getUserByMobile(selectUserInput);
		
		if (map == null) {
			
			userDao.saveEntity(selectUserInput);
			
		}
		
		return UserConstants.SUCCESS;
		
	}

	/**
	 * 用户登陆
	 * 
	 * <p>
	 * <b>Release Notes:</b> <br/>
	 * <table border="1" cellspacing="0" cellpadding="5" width="80%">
	 * <tr>
	 * <th align="left">Release</th>
	 * <th align="left">Notes</th>
	 * </tr>
	 * <tr>
	 * <td>@since 1.0</td>
	 * <td>&nbsp;</td>
	 * </tr>
	 * </table>
	 * <p>
	 * 
	 * @param mobile
	 *            <code>String</code> 手机号码
	 * @return <code>int</code> 成功或失败
	 * @throws Exception 
	 */
	@Override
//	public Map<String, Object> handleUserLogin(String mobile, int companyId) throws Exception {
	public Map<String, Object> handleUserLogin(String mobile, int companyId,String password) throws Exception {
		
		JSONObject json = new JSONObject();
		json.put("reason", UserConstants.SUCCESS);
		
		if (StringUtils.isNotBlank(mobile)) {
			mobile = mobile.trim();
		} else {
			// 参数异常
			
			json.put("reason", UserConstants.PARAM_ERROR);
			
			return json;
		}
		
		SelectUserInput selectUserInput = new SelectUserInput();
		
		selectUserInput.setMobile(mobile);
		
		selectUserInput.setCompanyId(companyId);
		
		Map<String, Object> userMap = userDao.getUserByMobile(selectUserInput);
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		if(userMap != null){
			
			String approve = userMap.get("approve").toString();
			
			if(approve.equals("2")){
				
				map.put("reason", UserConstants.PASSWD_INVALID);
				
				return map;
				
			}else if(approve.equals("0")){
				
				map.put("reason", UserConstants.ISNOTREGIT);
				
				return map;
			}else if(approve.equals("1")){
				
				List<Map<String, Object>> userRoleList = userDao.selectUserRole(selectUserInput);
				
				if(userRoleList != null && userRoleList.size()>0){
					
					Map<String, Object> userRole = userRoleList.get(0);
					
					if(userRole.get("roleType").toString().equals("O") || userRole.get("roleType").toString().equals("M")){
						
						int id = Integer.parseInt(userMap.get("id").toString());
						
						List<Map<String,Object>> lst = userDao.getUserForProcessName(id);
						
						
						if(lst != null && lst.size() > 0){
							
							if(lst.get(0).get("processName") != null ){
								
								map = userDao.getUserByMobileTPF(selectUserInput);
								
							}else {
								
								map = userDao.getUserByMobileOM(selectUserInput);
								
							}
							
						}
						
					}else if(userRole.get("roleType").toString().equals("T") || userRole.get("roleType").toString().equals("P") || userRole.get("roleType").toString().equals("F")){
						
						map = userDao.getUserByMobileTPF(selectUserInput);
						
					}else{
						
						map = userDao.getUserByMobile(selectUserInput);
					}
					//TODO
					if (!password.equals(userMap.get("password").toString())) {
						// 密码不正确
						
						map.put("reason", UserConstants.ERROR_OLDPASSWD);
						
						return map;
						
					}
					
					Object roleType = userRole.get("roleType");
					
					map.put("roleType", roleType.toString());
					
					
					map.put("reason", UserConstants.SUCCESS);
					

//					Map<String, Object> mapCache = (Map<String, Object>)ehcacheUtil.getCacheElement(userRole.get("id").toString());
//					
//					if(mapCache == null){
//						
//						map = new HashMap<String, Object>();
//						
//						map.put("roleType",userRole.get("roleType").toString());
//						
//						ehcacheUtil.addToCache(userRole.get("id").toString(), map);
//						
//					}
					
				}
			}
		}else {
			
			map.put("reason", UserConstants.USER_NOT_EXIST);
		}

		return map;
	}


	@Override
	public int isRegist(SelectUserInput selectUserInput) throws Exception {

		if (StringUtils.isEmpty(selectUserInput.getMobile()) || selectUserInput.getCompanyId() == 0) {

			return UserConstants.USR_EXCEPTIONS;
		}
		if (!StringUtil.isPhoneNum(selectUserInput.getMobile())) {

			return UserConstants.ISNOT_PHONE;
		}
		Map<String, Object> map = userDao.getUserByMobile(selectUserInput);

		if (null == map) {

			return UserConstants.USER_NOT_EXIST;
		}
		
		return UserConstants.SUCCESS;

	}
	
	@Override
	public void insertOrganization(InsterOrganization insterOrganization){
		
		
		userDao.insertOrg(insterOrganization);
		
	}
	
	
	@Override
	public List selectOrganization(){
		
		
		return userDao.selectOrganization();
	}

	@Override
	public List getRole() {
		
		return userDao.getRole();
	}

	@Override
	public List getUserByMobileAndName(SelectUserInput selectUserInput) {
		
		return userDao.getUserByMobileAndName(selectUserInput);
	} 
	@Override
	public List getUserByRoleType(SelectUserInput selectUserInput) {
		
		return userDao.getUserByRoleType(selectUserInput);
	} 
	
	@Override
	public List getRoleTypeByUserId(SelectUserInput selectUserInput) {
		
		return userDao.getRoleTypeByUserId(selectUserInput);
	} 
	
	@Override
	public List getProcessNameByUser(int id) {
		
		List<Map<String,Object>> lst = new ArrayList();
		
		lst = userDao.getUserForProcessName(id);
		
		if(lst.get(0).get("processName") == null){
			
			lst = userDao.getUserForProcessNameForTeam(id);
			
		}
		
		return lst;
	} 
	@Override
	public List getUserByProcessId(ProductOrder productOrder) {
			
		return userDao.getUserByProcessId(productOrder);
	} 
}
